﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Deployment;
using System.Deployment.Application;

namespace QCCure
{
    public partial class FormHelp : Form
    {
        public AboutBox AboutBox { get; set; }
        public bool Terminate { get; set;  }
        public FormHelp()
        {
            InitializeComponent();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void FormHelp_Shown(object sender, EventArgs e)
        {
            LoadHelp();
        }

        private void buttonLoadOnlineHelp_Click(object sender, EventArgs e)
        {
            LoadHelp();
        }

        private void LoadHelp()
        {
            try 
            {
                webBrowserContent.DocumentText = RepalceHelpText(Properties.Resources.Help);
            }
            catch (Exception ex)
            {
                 MessageBox.Show("Unable to load the help content due to: " + ex.Message);
            }
        }


        public string RepalceHelpText(string contents)
        {
            contents = contents.Replace("${help.login}", Properties.Resources.HelpLogin);
            contents = contents.Replace("${help.defect.filter}", Properties.Resources.HelpDefectFilter);
            contents = contents.Replace("${help.viewing.defect}", Properties.Resources.HelpViewingDefect);
            contents = contents.Replace("${help.saving.defect}", Properties.Resources.HelpSavingDefect);
                

            contents = contents.Replace("${app.title}", AboutBox.AssemblyTitle);
            contents = contents.Replace("${app.company}", AboutBox.AssemblyCompany);

            if (ApplicationDeployment.IsNetworkDeployed)
            {
                string dataDirectory = ApplicationDeployment.CurrentDeployment.DataDirectory;
                Version version = ApplicationDeployment.CurrentDeployment.CurrentVersion;
                contents = contents.Replace("${app.version}", version.ToString());
                contents = contents.Replace("${app.web.path}", ApplicationDeployment.CurrentDeployment.DataDirectory + Path.DirectorySeparatorChar + Properties.Settings.Default.helpFile);
                contents = contents.Replace("${app.dataDirectory}", dataDirectory);
            }
            else
            {
                contents = contents.Replace("${app.version}", AboutBox.AssemblyVersion);
                contents = contents.Replace("${app.web.path}", "");
                contents = contents.Replace("${app.dataDirectory}", "");
            }

            return contents;
        }

        private void FormHelp_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Terminate)
            {
                return;
            }
            else
            {
                e.Cancel = true;
                this.Hide();
            }
        }
    }
}
